import { DefaultLabelFormatterCallbackParams, LinearGradientObject } from 'echarts';

// 根据x/x2/y/y2计算角度, 返回css角度deg
export const getDeg = (x: number, x2: number, y: number, y2: number): number => {
  const x1 = x || 0;
  const y1 = y || 0;
  const x3 = x2 || 0;
  const y3 = y2 || 0;

  const k = (y3 - y1) / (x3 - x1);

  return Math.atan(k) * (180 / Math.PI);
};

// 根据Echarts的回调获取颜色
export const getColor = (
  color: DefaultLabelFormatterCallbackParams['color']
): React.CSSProperties['backgroundColor'] => {
  if (!color) {
    return color;
  }

  // ColorString
  if (typeof color === 'string') {
    return color;
  }

  // LinearGradientObject
  if ((color as LinearGradientObject).type === 'linear') {
    return `linear-gradient(${getDeg(
      (color as LinearGradientObject).x,
      (color as LinearGradientObject).x2,
      (color as LinearGradientObject).y,
      (color as LinearGradientObject).y2
    )}deg, ${(color as LinearGradientObject).colorStops
      .map((v) => `${v.color} ${v.offset * 100}%`)
      .join(',')})`;
  }
};
